home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / DSp Context Switch / DSp Context Switch Main.c next >
Encoding:
Text File  |  2000-09-28  |  19.1 KB  |  681 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        DSp Context Switch Main.c
  3.  
  4.     Contains:    Source file for demonstration of DSp 1.7 or 1.1.4 context switching
  5.                 right arrow for a larger context; left arrow for smaller; Cmd-Q to exit.
  6.  
  7.     Written by:    Geoff Stahl (ggs)
  8.  
  9.     Copyright:    Copyright (c) 1999 Apple Computer, Inc., All Rights Reserved.
  10.  
  11.     Change History (most recent first):
  12.  
  13.         <3+>    10/11/99    ggs     
  14.          <3>    10/10/99    ggs     Header clean up
  15.          <2>    10/10/99    ggs     Added support for DSP 1.1.4  with code that does the equivalent
  16.                                     of context switching
  17.          <1>    10/10/99    ggs     Initial Add
  18.  
  19.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  20.                 ("Apple") in consideration of your agreement to the following terms, and your
  21.                 use, installation, modification or redistribution of this Apple software
  22.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  23.                 please do not use, install, modify or redistribute this Apple software.
  24.  
  25.                 In consideration of your agreement to abide by the following terms, and subject
  26.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  27.                 copyrights in this original Apple software (the "Apple Software"), to use,
  28.                 reproduce, modify and redistribute the Apple Software, with or without
  29.                 modifications, in source and/or binary forms; provided that if you redistribute
  30.                 the Apple Software in its entirety and without modifications, you must retain
  31.                 this notice and the following text and disclaimers in all such redistributions of
  32.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  33.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  34.                 Apple Software without specific prior written permission from Apple.  Except as
  35.                 expressly stated in this notice, no other rights or licenses, express or implied,
  36.                 are granted by Apple herein, including but not limited to any patent rights that
  37.                 may be infringed by your derivative works or by other works in which the Apple
  38.                 Software may be incorporated.
  39.  
  40.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  41.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  42.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  44.                 COMBINATION WITH YOUR PRODUCTS.
  45.  
  46.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  47.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  48.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  49.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  50.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  51.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  52.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  53. */
  54.  
  55.  
  56. // system includes ----------------------------------------------------------
  57.  
  58. #include <Events.h>
  59. #include <Fonts.h>
  60. #include <Windows.h>
  61. #include <Dialogs.h>
  62. #include <ToolUtils.h>
  63. #include <Devices.h>
  64.  
  65. #include <DrawSprocket.h>
  66.  
  67. #include <stdio.h>
  68.  
  69. // project includes ---------------------------------------------------------
  70.  
  71.  
  72. // function declarations ----------------------------------------------------
  73.  
  74. void InitToolbox(void);
  75. void GraphicsInitAttributes(DSpContextAttributes *inAttributes);
  76. Boolean SetUp (void);
  77. void DoMenu (SInt32 menuResult);
  78. void SwitchDSpContext(short context);
  79. void DoKey (SInt8 theKey, SInt8 theCode);
  80. void CToPStr (register const char *inString, Str255 * outString );
  81. void DoUpdate (void);
  82. void DoEvent (void);
  83. void CleanUp (void);
  84.  
  85. // statics/globals (internal only) ------------------------------------------
  86.  
  87. // Menu defs
  88. enum 
  89. {
  90.     kNumContexts = 8,
  91.     kBitsPerPixel = 16,
  92.  
  93.     kMenuApple = 128,
  94.     kMenuFile = 129,
  95.     
  96.     kAppleAbout = 1,
  97.     kFileDoNothing = 1,
  98.     kFileQuit
  99. };
  100.  
  101. Boolean gDone = false;
  102. UInt32 gSleepTime = 1;
  103.  
  104. RGBColor grgbColor = {0x5000,0x5000,0x5000};
  105.  
  106. short gContextCurrent = 0;
  107. short gActualNumContexts = 0;
  108. Point gapntResolution [kNumContexts] = {{480, 640}, {600, 800}, {624, 832}, {768, 1024}, {870, 1152}, {1024, 1280}, {1600, 1200}, {1920, 1440}};
  109. DSpContextAttributes gaContextAttributes [kNumContexts];
  110. DSpContextReference gaContextRef [kNumContexts];
  111. DSpContextReference * gpContextRefUnused = NULL;
  112. unsigned long gDisplayID;
  113. GDHandle ghGD;
  114. short gnumDevices = 0;
  115. Boolean gfDSp17 = true;
  116.  
  117.  
  118.  
  119. // functions (internal/private) ---------------------------------------------
  120.  
  121. void InitToolbox (void)
  122. {
  123.     MenuHandle menu;
  124.     SInt16 modifiers = 0;
  125.     EventRecord event;
  126.  
  127.     MaxApplZone ();
  128.  
  129.     InitGraf((Ptr) &qd.thePort);
  130.     InitFonts();
  131.     InitWindows();
  132.     InitMenus();
  133.     TEInit();
  134.     InitDialogs(nil);
  135.     InitCursor();
  136.     
  137.     qd.randSeed =  (long) TickCount();
  138.  
  139.     // init events
  140.     EventAvail(everyEvent, &event);
  141.     modifiers |= event.modifiers;
  142.     EventAvail(everyEvent, &event);
  143.     modifiers |= event.modifiers;
  144.     EventAvail(everyEvent, &event);
  145.     modifiers |= event.modifiers;
  146.  
  147.     // Init Menus
  148.     menu = NewMenu (kMenuApple, "\p\024");            // new  apple menu
  149.     InsertMenu (menu, 0);                            // add menu to end
  150.     AppendResMenu(menu, 'DRVR');
  151.     
  152.     menu = NewMenu (kMenuFile, "\pFile");            // new menu
  153.     InsertMenu (menu, 0);                            // add menu to end
  154.     AppendMenu (menu, "\pDo Nothing;Quit/Q"); // add items
  155.     
  156.     DrawMenuBar();
  157.  
  158. // --------------------------------------------------------------------------
  159.  
  160. void GraphicsInitAttributes(DSpContextAttributes *inAttributes)
  161. {
  162.     BlockZero (inAttributes, sizeof (DSpContextAttributes));
  163. }
  164.  
  165. // --------------------------------------------------------------------------
  166.  
  167. Boolean SetUp (void)
  168. {
  169.     OSStatus err = noErr;
  170.     short i, context;
  171.     
  172.     InitToolbox ();
  173.     
  174.     // Do DrawSprocket Setup
  175.     
  176.     if ((Ptr) kUnresolvedCFragSymbolAddress == (Ptr) DSpStartup)
  177.     {
  178.         DebugStr("\pDSp extension not loaded\n");
  179.         return false;
  180.     }
  181.     if (DSpStartup () != noErr)
  182.     {
  183.         DebugStr("\pUnable to startup DSp\n");
  184.         return false;
  185.     }
  186.     if ((Ptr) kUnresolvedCFragSymbolAddress == (Ptr) DSpGetVersion) 
  187.         gfDSp17 = false;
  188.     else
  189.     {
  190.         // do not use built in DSpContext_Reserve and DSpContext_Queue prior to DSp 1.7.3
  191.         NumVersion version = DSpGetVersion ();
  192.         if ((version.majorRev < 0x01)  || ((version.majorRev == 0x01) && (version.minorAndBugRev < 0x73)))
  193.             gfDSp17 = false;
  194.     }
  195.             
  196.     context = 0;
  197.     for (i = 0; i < kNumContexts; i++)    // get all of our possible contexts kNumContexts
  198.     {
  199.         GraphicsInitAttributes (&gaContextAttributes [context]);
  200.         
  201.         gaContextAttributes [context].displayWidth            = gapntResolution [i].h;
  202.         gaContextAttributes [context].displayHeight            = gapntResolution [i].v;
  203.         gaContextAttributes [context].colorNeeds            = kDSpColorNeeds_Require;
  204.         gaContextAttributes [context].displayBestDepth        = kBitsPerPixel;
  205.         gaContextAttributes [context].displayDepthMask        = kDSpDepthMask_All;
  206.         // this is required prior to DSp 1.7.3, even if you only need one buffer
  207.         gaContextAttributes [context].backBufferBestDepth    = kBitsPerPixel;
  208.         gaContextAttributes [context].backBufferDepthMask    = kDSpDepthMask_All;
  209.         gaContextAttributes [context].pageCount                = 1;                                // only the front buffer is needed
  210.         // use two above if you want to swap buffers to animate
  211.         
  212.         if (0 == context)    // for the first context
  213.         {
  214.             // will display user dialog if context selection is required otherwise as find best context
  215.             err = DSpUserSelectContext(&gaContextAttributes [context], 0, nil, &gaContextRef[context]);
  216.             if (err != noErr)
  217.             {
  218. //                DebugStr("\pA suitable display context could not be found.");
  219.             }
  220.  
  221.             if (noErr != DSpContext_GetAttributes (gaContextRef[context], &gaContextAttributes [context])) // see what we actually found
  222.             {
  223.                 DebugStr("\pUnable to get attributes\n");
  224.             }
  225.             gaContextAttributes [context].displayWidth        = gapntResolution [i].h;
  226.             gaContextAttributes [context].displayHeight        = gapntResolution [i].v;
  227.             gaContextAttributes [context].pageCount            = 1;                                    // only the front buffer is needed
  228.             gaContextAttributes [context].contextOptions    = 0 | kDSpContextOption_DontSyncVBL;    // no page flipping and no VBL sync needed
  229.  
  230.             // get our device for future use
  231.             err =  DSpContext_GetDisplayID (gaContextRef [context], &gDisplayID);
  232.             if (err != noErr)
  233.             {
  234.                 DebugStr("\pDSpContext_GetDisplayID () had an error.");
  235.                 return false;
  236.             }
  237.  
  238.             // get GDHandle for ID'd device
  239.             err = DMGetGDeviceByDisplayID (gDisplayID, &ghGD, false);
  240.             if (err != noErr)
  241.             {
  242.                 DebugStr("\pDMGetGDeviceByDisplayID() had an error.");
  243.                 return false;
  244.             }
  245.             
  246.             if (false == gfDSp17)    // reserve contexts on other screens to prevent selection
  247.             {
  248.                 GDHandle hDevice = DMGetFirstScreenDevice (true); // check number of screens
  249.                 short indexDevice = 0;
  250.                 do
  251.                 {
  252.                     gnumDevices++;
  253.                     hDevice = DMGetNextScreenDevice (hDevice, true);
  254.                 }
  255.                 while (hDevice);
  256.                 gnumDevices--; // only count unsued screens
  257.                 if (gnumDevices)
  258.                 {
  259.                     gpContextRefUnused = (DSpContextReference *) NewPtr ((long) sizeof (DSpContextReference) * gnumDevices);
  260.                     hDevice = DMGetFirstScreenDevice (true); // check number of screens
  261.                     do
  262.                     {
  263.                         if (hDevice != ghGD)    // if this device is not the one the user chose
  264.                         {
  265.                             unsigned long displayID;
  266.                             DSpContextAttributes contextAttributes;
  267.                             err = DMGetDisplayIDByGDevice (hDevice, &displayID, false);
  268.                             err = DSpGetFirstContext (displayID, &gpContextRefUnused [indexDevice]); // get a context and
  269.                             err = DSpContext_GetAttributes (gpContextRefUnused [indexDevice], &contextAttributes); // find attributes
  270.                             err = DSpContext_Reserve (gpContextRefUnused [indexDevice], &contextAttributes); // reserve it
  271.                             indexDevice++;
  272.                         }
  273.                         hDevice = DMGetNextScreenDevice (hDevice, true);
  274.                     }
  275.                     while (hDevice);
  276.                 }
  277.             }
  278.         }
  279.         else
  280.         {
  281.             // find a context on the display used in the first context
  282.             if (false == gfDSp17)
  283.                 err = DSpFindBestContext (&gaContextAttributes [context], &gaContextRef [context]);
  284.             else // in this case we have reserved contexts on all other screens thus they will not be considered
  285.                 err = DSpFindBestContextOnDisplayID (&gaContextAttributes [context], &gaContextRef [context], gDisplayID);
  286.             if (noErr == err) 
  287.             {
  288.                 if (noErr != DSpContext_GetAttributes (gaContextRef[context], &gaContextAttributes [context])) // see what we actually found
  289.                 {
  290.                     DebugStr("\pUnable to get attributes\n");
  291.                 }
  292.                 gaContextAttributes [context].displayWidth        = gapntResolution [context].h;
  293.                 gaContextAttributes [context].displayHeight        = gapntResolution [context].v;
  294.                 gaContextAttributes [context].pageCount            = 1;                                    // only the front buffer is needed
  295.                 gaContextAttributes [context].contextOptions    = 0 | kDSpContextOption_DontSyncVBL;    // no page flipping and no VBL sync needed
  296.             }
  297.             else
  298.             {
  299. //                DebugStr( "\pDSpFindBestContextOnDisplayID could not find context");
  300.             }
  301.         }
  302.         if (gaContextRef[context])    // if we use that slot
  303.         {
  304.             context++;
  305.             gActualNumContexts++;
  306.         }
  307.     }
  308.     gContextCurrent = 0;    // start at the first
  309.     
  310.     // reserve our context
  311.     err = DSpContext_Reserve (gaContextRef [gContextCurrent], &gaContextAttributes [gContextCurrent]);
  312.     if (err != noErr)
  313.     {
  314.         DebugStr("\pUnable to create the display!");
  315.         return false;
  316.     }
  317.  
  318.     if (false == gfDSp17)
  319.     {
  320.         // do nothing
  321.     }
  322.     else
  323.     {
  324.         for (i = 1; i < gActualNumContexts; i++)    // get all of our contexts
  325.         {
  326.             // queue it for switch
  327.             err = DSpContext_Queue(gaContextRef [gContextCurrent], gaContextRef [i], &gaContextAttributes [i]);
  328.             if (err) 
  329.                 DebugStr( "\pDSpContext_Queue() had an error.");
  330.         }
  331.     }
  332.  
  333.     DSpSetBlankingColor(&grgbColor);
  334.     HideCursor();
  335.  
  336.     err = DSpContext_FadeGammaOut (NULL, NULL);                    // remove for debug
  337.     if (err != noErr)
  338.         DebugStr("\pUnable to fade the display!");
  339.  
  340.     // activate our context
  341.     err = DSpContext_SetState (gaContextRef [gContextCurrent], kDSpContextState_Active);
  342.     if (err != noErr)
  343.     {
  344.         DebugStr("\pUnable to set the display!");
  345.         return false;
  346.     }
  347.         
  348.     err = DSpContext_FadeGammaIn (NULL, NULL);    
  349.     if(err)
  350.         DebugStr("\pUnable to fade the display!");
  351.  
  352.     return true;
  353. }
  354.  
  355. // --------------------------------------------------------------------------
  356.  
  357. void DoMenu (SInt32 menuResult)
  358. {
  359.     SInt16 theMenu;
  360.     SInt16 theItem;
  361.     Str255 daName;
  362.     MenuRef theMenuHandle;
  363.         
  364.     theMenu = HiWord(menuResult);
  365.     theItem = LoWord(menuResult);
  366.     theMenuHandle = GetMenuHandle(theMenu);
  367.  
  368.     switch (theMenu)
  369.     {
  370.         case kMenuApple:
  371.             switch (theItem)
  372.             {
  373.                 case kAppleAbout:
  374.                     break;
  375.                 default:
  376.                     OpenDeskAcc(daName);
  377.                     break;
  378.             }
  379.             break;
  380.         case kMenuFile:
  381.             switch (theItem)
  382.             {
  383.                 case kFileDoNothing:
  384.                     break;
  385.                 case kFileQuit:
  386.                     gDone = true;
  387.                     break;
  388.             }
  389.             break;
  390.     }
  391.     HiliteMenu(0);
  392.     DrawMenuBar();
  393. }
  394.  
  395. // --------------------------------------------------------------------------
  396.  
  397. void SwitchDSpContext(short context)
  398. {
  399.     OSStatus err;
  400.     if(gContextCurrent != context)
  401.     {    
  402.         if(gaContextRef[gContextCurrent] && gaContextRef[context]) 
  403.         {        
  404.             if (false == gfDSp17)
  405.             {
  406.                 // fade out
  407.                 err = DSpContext_FadeGammaOut (NULL, NULL); // remove for debug
  408.                 if (err != noErr)
  409.                     DebugStr("\pUnable to fade the display!");
  410.  
  411.                 // deactivate our context
  412.                 err = DSpContext_SetState (gaContextRef [gContextCurrent], kDSpContextState_Inactive);
  413.                 if (err != noErr)
  414.                     DebugStr("\pUnable to set the display!");
  415.                     
  416.                 // release
  417.                 err = DSpContext_Release (gaContextRef [0]);
  418.                 if (err != noErr) 
  419.                     DebugStr ("\pDSpContext_Release error");
  420.                     
  421.                 // reserve
  422.                 err = DSpContext_Reserve (gaContextRef [context], &gaContextAttributes [context]);
  423.                 if (err != noErr)
  424.                     DebugStr("\pUnable to create the display!");
  425.                 
  426.                 // activate
  427.                 err = DSpContext_SetState (gaContextRef [context], kDSpContextState_Active);
  428.                 if (err != noErr)
  429.                     DebugStr("\pUnable to set the display!");
  430.  
  431.                 // fade in
  432.                 err = DSpContext_FadeGammaIn (NULL, NULL);    
  433.                 if(err)
  434.                     DebugStr("\pUnable to fade the display!");
  435.                 
  436.                 if(err) 
  437.                     DebugStr ("\pUnable to switch displays");
  438.                 else
  439.                     gContextCurrent = context;
  440.             }
  441.             else
  442.             {
  443.                 err = DSpContext_Switch(gaContextRef[gContextCurrent], gaContextRef[context]);
  444.                 if(err) 
  445.                      DebugStr ("\pUnable to switch displays");
  446.                 else
  447.                     gContextCurrent = context;
  448.             }
  449.         }
  450.     }
  451. }    
  452.  
  453. // --------------------------------------------------------------------------
  454.  
  455. void DoKey (SInt8 theKey, SInt8 theCode)
  456. {
  457.     #pragma unused (theCode)
  458.     short context = gContextCurrent;
  459.     if (theKey == '\35')
  460.     {
  461.     
  462.         if (++context == gActualNumContexts)
  463.             context = 0;
  464.         SwitchDSpContext (context);
  465.         DoUpdate ();
  466.     }
  467.     else if (theKey == '\34')
  468.     {
  469.         if (--context < 0)
  470.             context = (short) (gActualNumContexts - 1);
  471.         SwitchDSpContext (context);
  472.         DoUpdate ();
  473.     }
  474. }
  475.  
  476. //---------------------------------------------------------------------------
  477.  
  478. // Copy C string to Pascal string
  479.  
  480. void CToPStr (register const char *inString, Str255 * outString )        // copies in to out
  481. {    
  482.     register unsigned char x = 0;
  483.     do
  484.     {
  485.         *(((char*)outString) + 1 + x) = *(inString + x);
  486.         x++;
  487.     }
  488.     while ((*(inString + x) != 0)  && (x < 256));
  489.     *((char*)outString) = (char) x;                                    
  490. }
  491.  
  492. // --------------------------------------------------------------------------
  493.  
  494. void DoUpdate (void)
  495. {
  496.     OSStatus err;
  497.     CGrafPtr pCGrafFrontBuffer, pCGrafSave = NULL;
  498.     GDHandle hGDSave;
  499.     RGBColor rgbSave, rgbRed = {0x7FFF, 0x0000, 0x0000}, rgbWhite = {0xFFFF, 0xFFFF, 0xFFFF};
  500.     short fNum, len;
  501.     Str255 aStr = "\p";
  502.     char aChar[256];
  503.  
  504.     err = DSpContext_GetFrontBuffer (gaContextRef [gContextCurrent], &pCGrafFrontBuffer);    
  505.     if (err != noErr)
  506.     {
  507.         DebugStr("\pUnable to get front buffer");
  508.         return;
  509.     }
  510.         
  511.     GetGWorld (&pCGrafSave, &hGDSave);
  512.     SetGWorld (pCGrafFrontBuffer, ghGD);
  513.     GetForeColor (&rgbSave);
  514.     
  515.     // draw here
  516.     RGBForeColor (&rgbRed);
  517.     PaintRect (&pCGrafFrontBuffer->portRect);
  518.  
  519.     RGBForeColor (&rgbWhite);
  520.  
  521.     // set text
  522.     GetFNum("\pGeneva", &fNum);
  523.     TextFont(fNum);
  524.     TextSize(12);
  525.     
  526.     // draw centered to screen
  527.     sprintf (aChar, "%d x %d", gapntResolution [gContextCurrent].h, gapntResolution [gContextCurrent].v);
  528.     CToPStr (aChar, &aStr);
  529.     len = StringWidth (aStr);
  530.      MoveTo ((short) (pCGrafFrontBuffer->portRect.left + (pCGrafFrontBuffer->portRect.right - pCGrafFrontBuffer->portRect.left) / 2 - (len / 2)), 18);
  531.     DrawString(aStr);
  532.     
  533.     RGBForeColor (&rgbSave);
  534.     SetGWorld (pCGrafSave, hGDSave);
  535. }
  536.  
  537. // --------------------------------------------------------------------------
  538.  
  539. void DoEvent (void)
  540. {
  541.     EventRecord theEvent;
  542.     SInt16 whatPart;
  543.     SInt32 menuResult;
  544.     WindowRef whichWindow;
  545.     SInt8 theKey;
  546.     SInt8 theCode;
  547.     Boolean fProcessed;
  548.     
  549.     if (WaitNextEvent(everyEvent, &theEvent, gSleepTime, NULL))
  550.     {
  551.         DSpProcessEvent (&theEvent, &fProcessed);
  552.         if (!fProcessed)
  553.         {
  554.             switch (theEvent.what)
  555.             {
  556.                 case mouseDown:
  557.                     whatPart = FindWindow(theEvent.where, &whichWindow);
  558.                     switch (whatPart)
  559.                     {
  560.                         case inGoAway:
  561.                             break;
  562.                         case inMenuBar:
  563.                             DrawMenuBar();
  564.                             menuResult = MenuSelect(theEvent.where);
  565.                             if (HiWord(menuResult) != 0)
  566.                                 DoMenu(menuResult);
  567.                             break;
  568.                         case inSysWindow:
  569.                             SystemClick(&theEvent, whichWindow);
  570.                             break;
  571.                         case inContent:
  572.                             break;
  573.                     }
  574.                     break;
  575.                 case keyDown:
  576.                 case autoKey:
  577.                     theKey = (char)(theEvent.message & charCodeMask);
  578.                     theCode = (char)((theEvent.message & keyCodeMask) >> 8);
  579.                     if ((theEvent.modifiers & cmdKey) != 0)
  580.                     {
  581.                         menuResult = MenuKey(theKey);
  582.                         if (HiWord(menuResult) != 0)
  583.                             DoMenu (menuResult);
  584.                     }
  585.                     else
  586.                         DoKey (theKey, theCode);
  587.                     break;
  588.                 case updateEvt:
  589.                     BeginUpdate((WindowRef) theEvent.message);
  590.                     DoUpdate();
  591.                     EndUpdate((WindowRef) theEvent.message);
  592.                     break;
  593.                 case diskEvt:
  594.                     break;
  595.                 case osEvt:
  596.                     if (theEvent.message & 0x01000000)        //    Suspend/resume event
  597.                     {
  598.                         if (theEvent.message & 0x00000001)    //    Resume
  599.                         {
  600.                             gSleepTime = 1;                //    Suspend                            
  601.                             DoUpdate ();
  602.                         }
  603.                         else
  604.                             gSleepTime = 32000;                //    Suspend
  605.                     }
  606.                     break;
  607.  
  608.                 case kHighLevelEvent:
  609.                     AEProcessAppleEvent(&theEvent);
  610.                     break;
  611.             }
  612.         }
  613.     }
  614.     else
  615.     {
  616.         // idle tasks
  617.     }
  618. }
  619.  
  620. // --------------------------------------------------------------------------
  621.  
  622. void CleanUp (void)
  623. {
  624.     OSStatus err;
  625.  
  626.     err = DSpContext_FadeGammaOut (NULL, NULL);                    // remove for debug
  627.     if (err != noErr)
  628.         DebugStr("\pUnable to fade the display!");
  629.  
  630.     err = DSpContext_SetState (gaContextRef [gContextCurrent], kDSpContextState_Inactive);
  631.     if (err != noErr)
  632.         DebugStr("\pUnable to set the display!");
  633.         
  634.     err = DSpContext_FadeGammaIn (NULL, NULL);    
  635.     if(err)
  636.         DebugStr("\pUnable to fade the display!");
  637.  
  638.     if (false == gfDSp17)
  639.     {
  640.         err = DSpContext_Release (gaContextRef [gContextCurrent]);
  641.         if (err != noErr) 
  642.             DebugStr ("\pDSpContext_Release error");
  643.             
  644.         while (gnumDevices--) // dump our reserved unused devices
  645.         {
  646.             err = DSpContext_Release (gpContextRefUnused [gnumDevices]);
  647.             if (err != noErr) 
  648.                 DebugStr ("\pDSpContext_Release error");
  649.         }
  650.     }
  651.     else
  652.     {
  653.         err = DSpContext_Release (gaContextRef [0]);
  654.         if (err != noErr) 
  655.             DebugStr ("\pDSpContext_Release error");
  656.     }
  657.     
  658.     err = DSpShutdown ();
  659.     if (err != noErr) 
  660.         DebugStr ("\pDSpShutdown error");
  661. }
  662.  
  663. // --------------------------------------------------------------------------
  664.  
  665. int main (void)
  666. {
  667.     if (SetUp ())
  668.     {
  669.         DoUpdate ();
  670.         while (!gDone) 
  671.         {
  672.             DoEvent ();
  673.         }
  674.     }
  675.     CleanUp ();
  676.     return 0;
  677. }
  678.  
  679.  
  680.